precision mediump float;
uniform sampler2D u_texture;
uniform sampler2D u_normalmap;
uniform vec3 u_camPos;
uniform vec3 u_light_color;
uniform vec3 u_light_position;


//uniform vec3 u_ambient_color;
//uniform vec3 u_specular_color;
//uniform float u_weight;

varying vec3 v_normW;
varying vec3 v_posW;
varying vec2 v_uv;
varying vec3 v_tangentW;
varying vec3 v_bitangentW;
void main()
{
	float weight = 0.2;
	vec4 u_ambient_color = vec4(1.0, 1.0, 1.0, 1.0);
	vec4 u_specular_color =vec4(1.0, 1.0, 1.0, 1.0);
	vec4 norm_tex = texture2D(u_normalmap, v_uv);
	
	mat3 TBN = mat3(normalize(v_tangentW), normalize(v_bitangentW), normalize(v_normW));
	vec3 norm = normalize(TBN * (2.0 * norm_tex.xyz - 1.0));
	vec3 light = normalize(v_posW - u_light_position);
	//vec3 norm = normalize(v_normW);
	vec4 object_color = texture2D(u_texture, v_uv);
	
	vec3 to_eye = normalize(u_camPos - v_posW);
	vec4 diffuse = vec4(u_light_color * max(0.0, dot(norm, -light)), 1.0);
	
	vec3 reflect_direction = normalize(reflect(light, norm));
	vec4 specular = pow(max(dot(reflect_direction, to_eye), 0.0), 20.0) * u_specular_color;
	
	gl_FragColor = vec4(((u_ambient_color * weight + diffuse * (1.0 - weight)) * object_color + specular).xyz, object_color.w);
	//gl_FragColor.xyz = v_tangentW;
}
